import 'package:flutter/material.dart';
import 'package:x3_base_core/x3_base_core.dart';
import 'package:x3_component_core/x3_component_core.dart';
import 'package:x3_form_core/models/vip_select_model.dart';
import 'package:x3_form_core/models/user_select_model.dart';
import 'package:x3_form_core/provider/base_request_form_provider.dart';
import 'package:x3_form_core/business_components/base_request_form_date.dart';
import 'package:x3_form_core/business_components/base_request_form_image.dart';
import 'package:x3_form_core/business_components/base_request_form_input.dart';
import 'package:x3_form_core/business_components/base_request_form_radio.dart';
import 'package:x3_form_core/business_components/base_request_form_link_input.dart';
import 'package:x3_form_core/business_components/base_request_form_input_area.dart';
import 'package:x3_form_core/business_components/base_request_form_select_down.dart';
import 'package:x3_form_core/business_components/base_request_form_input_switch.dart';
import 'package:x3_form_core/business_components/base_request_form_another_radio.dart';
import 'package:x3_form_core/business_components/base_request_form_select_dialog.dart';
import 'package:x3_form_core/business_components/base_request_form_address_dialog.dart';
import 'package:x3_form_core/business_components/base_request_form_business_select.dart';
import 'package:x3_form_core/business_components/base_request_form_appointment_time.dart';
import 'package:x3_form_core/business_components/base_request_form_input_with_button.dart';
import 'package:x3_form_core/business_components/base_request_form_multiple_select_dialog.dart';
import 'package:x3_form_core/business_components/base_request_form_business_select_and_jump_details.dart';

import 'base_request_form_business_input_search_choice.dart';
import 'base_request_form_scan_input_text.dart';

/// 通过入参构建表单
class BaseRequestForm extends StatefulWidget {

  BaseRequestForm({Key key, @required this.baseRequestFormProvider, this.requestSaveData, this.isSaveASingle = false}): assert(baseRequestFormProvider != null), super(key: key);

  /// 入参数据
  final BaseRequestFormProvider baseRequestFormProvider;

  /// 马上调服务保存单个 请求服务保存数据
  final RequestSaveData requestSaveData;

  /// 处理单个保存事件（单行文本，多行文本）
  final bool isSaveASingle;

  @override
  _BaseRequestFormState createState() => _BaseRequestFormState();

}

class _BaseRequestFormState extends State<BaseRequestForm> implements BaseCatalogRequest<BaseHttpInputTypeEnum> {

  void _initRequestFields() {
    List<BaseHttpRequestModel> requests = widget.baseRequestFormProvider.requests ?? [];
    if (requests.length > 0) {
      List<String> _names = widget.baseRequestFormProvider.requests.map((e) => e.name).toList();
      _names.sort((a, b) {
        return b.length - a.length;
      });
      String maxName = _names.first;
      TextSpan text = TextSpan(text: '*' + maxName, style: Theme.of(context).textTheme.bodyText2);
      TextPainter tp = TextPainter(textScaleFactor: 1, text: text, textDirection: TextDirection.ltr)..layout();
      if (tp.size.width <= BaseRequestFormProvider.maxLabelWidth) {
        widget.baseRequestFormProvider.setLabelWidth(width: tp.size.width);
      } else {
        widget.baseRequestFormProvider.setLabelWidth(width: BaseRequestFormProvider.maxLabelWidth);
      }
    }
  }

  @override
  Widget build(BuildContext context) {
    this._initRequestFields();
    return Provider(
      create: (_) => widget.baseRequestFormProvider,
      child: _buildContent(),
    );
  }

  Widget _buildContent() {
    return Consumer<BaseRequestFormProvider>(
      builder: (BuildContext context, BaseRequestFormProvider model, Widget child) {
        List<Widget> children = BaseCatalogController.catalogFormComponents(model.requests, model.dataSource, this.catalog());
        return ListView.separated(
          itemCount: children.length,
          itemBuilder: (context, index) => children[index],
          separatorBuilder: (context, index) => BaseComponentUtils.buildHorizontalDivider(),
          shrinkWrap: true,
          physics: NeverScrollableScrollPhysics(),
        );
      },
    );
  }

  /// 根据入参构建表单
  @override
  Map<BaseHttpInputTypeEnum, Widget Function(BaseHttpRequestModel request, Map<String, dynamic> dataSource)> catalog() {
    return {
      /// 文本
      BaseHttpInputTypeEnum.InputText: (request, dataSource) => _baseFormInput(request),

      /// 数字
      BaseHttpInputTypeEnum.InputNumber: (request, dataSource) => _baseFormInput(request),
      BaseHttpInputTypeEnum.InputAmount: (request, dataSource) => _baseFormInput(request),
      BaseHttpInputTypeEnum.InputFloat: (request, dataSource) => _baseFormInput(request),

      /// 密码
      BaseHttpInputTypeEnum.InputPassword: (request, dataSource) => _baseFormInput(request, isPassword: true),

      /// 发送验证码 文本
      BaseHttpInputTypeEnum.InputWithButton: (request, dataSource) => BaseRequestFormInputWithButton(request: request),

      /// 文本域
      BaseHttpInputTypeEnum.InputTextArea: (request, dataSource) => BaseRequestFormInputArea(request: request, isSaveASingle: widget.isSaveASingle, requestSaveData: widget.requestSaveData),

      /// 跳转页编辑
      BaseHttpInputTypeEnum.LinkInputText: (request, dataSource) => BaseRequestFormLinkInput(request: request, requestSaveData: widget.requestSaveData, isMultipleLines: false),

      /// 跳转页编辑
      BaseHttpInputTypeEnum.LinkInputTextArea: (request, dataSource) => BaseRequestFormLinkInput(request: request, requestSaveData: widget.requestSaveData, isMultipleLines: true),

      /// 单选框
      BaseHttpInputTypeEnum.Radio: (request, dataSource) => BaseRequestFormRadio(request: request, requestSaveData: widget.requestSaveData),

      /// 单选框 - 另外一个单选样式
      BaseHttpInputTypeEnum.AnOtherRadio: (request, dataSource) => BaseRequestFormAnOtherRadio(request: request, requestSaveData: widget.requestSaveData),

      /// 保存图片
      BaseHttpInputTypeEnum.UploadImage: (request, dataSource) => BaseRequestFormImage(request: request, requestSaveData: widget.requestSaveData),

      /// 选择日期
      BaseHttpInputTypeEnum.InputDate: (request, dataSource) => BaseRequestFormDate(request: request, requestSaveData: widget.requestSaveData),

      /// 选择日期（限制只能从今往后）
      BaseHttpInputTypeEnum.InputDateFromNowOn: (request, dataSource) => BaseRequestFormDate(request: request, requestSaveData: widget.requestSaveData, fromNowOn: true),

      /// 选择地址
      BaseHttpInputTypeEnum.ChooseAddress: (request, dataSource) => BaseRequestFormAddressDialog(request: request, requestSaveData: widget.requestSaveData),

      /// 开关
      BaseHttpInputTypeEnum.InputSwitch: (request, dataSource) => BaseRequestFormInputSwitch(request: request, requestSaveData: widget.requestSaveData),

      /// 带数据底部弹窗
      BaseHttpInputTypeEnum.SelectDown: (request, dataSource) => BaseRequestFormSelectDown(request: request, requestSaveData: widget.requestSaveData),

      /// 选择预约时间
      BaseHttpInputTypeEnum.SelectAppointmentTime: (request, dataSource) => BaseRequestFormAppointmentTime(request: request, requestSaveData: widget.requestSaveData),

      /// 底部单选
      BaseHttpInputTypeEnum.SelectDialog: (request, dataSource) => BaseRequestFormSelectDialog(request: request, requestSaveData: widget.requestSaveData),

      /// 底部多选
      BaseHttpInputTypeEnum.MultipleSelectDialog: (request, dataSource) => BaseRequestFormMultipleSelectDialog(request: request, requestSaveData: widget.requestSaveData),

      /// 选择用户 单选
      BaseHttpInputTypeEnum.UserSelect: (request, dataSource) => BaseRequestFormBusinessSelect<UserSelectModel>(request: request, requestSaveData: widget.requestSaveData),

      /// 多选
      BaseHttpInputTypeEnum.UserMultiSelect: (request, dataSource) => BaseRequestFormBusinessSelect<UserSelectModel>(request: request, requestSaveData: widget.requestSaveData, isMulti: true),

      /// 选择顾客 单选
      BaseHttpInputTypeEnum.VipSelect: (request, dataSource) => BaseRequestFormBusinessSelect<VipSelectModel>(request: request, requestSaveData: widget.requestSaveData),

      /// 选择顾客 单选 支持点击跳转到会员详情
      BaseHttpInputTypeEnum.VipSelectAndJumpDetails: (request, dataSource) => BaseRequestFormBusinessSelectAndJumpDetails<VipSelectModel>(request: request, requestSaveData: widget.requestSaveData),
      //  多选
      BaseHttpInputTypeEnum.VipMultiSelect: (request, dataSource) => BaseRequestFormBusinessSelect<VipSelectModel>(request: request, requestSaveData: widget.requestSaveData, isMulti: true),
      //  多选 支持点击跳转到会员详情
      BaseHttpInputTypeEnum.VipMultiSelectAndJumpDetails: (request, dataSource) => BaseRequestFormBusinessSelectAndJumpDetails<VipSelectModel>(request: request, requestSaveData: widget.requestSaveData, isMulti: true),

      /// 通用模糊搜索 - 单选
      BaseHttpInputTypeEnum.InputSearchSingleChoice: (request, dataSource) => BaseRequestFormBusinessInputSearchChoice(request: request, requestSaveData: widget.requestSaveData, isMulti: false),

      /// 通用模糊搜索 - 多选
      BaseHttpInputTypeEnum.InputSearchMultipleChoice: (request, dataSource) => BaseRequestFormBusinessInputSearchChoice(request: request, requestSaveData: widget.requestSaveData, isMulti: true),

      /// 扫描录入文本
      BaseHttpInputTypeEnum.ScanInputText: (request, dataSource) => BaseRequestFormScanInputText(request: request, requestSaveData: widget.requestSaveData),

    };
  }

  /// 通用文本表单
  BaseRequestFormInput _baseFormInput(BaseHttpRequestModel request, {bool isPassword = false}) {
    return BaseRequestFormInput(request: request, isPassword: isPassword, isSaveASingle: widget.isSaveASingle, requestSaveData: widget.requestSaveData);
  }
}
